package com.atkxp.stustatusmasystem.controller;

import com.atkxp.stustatusmasystem.pojo.*;
import com.atkxp.stustatusmasystem.pojo.Class;
import com.atkxp.stustatusmasystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author user
 * @declaration
 * @create 2023/5/25-20:49
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    public StudentService studentService;

    @Autowired
    public TeacherService teacherService;

    @Autowired
    public MajorService majorService;

    @Autowired
    public ClassService classService;

    @Autowired
    public ApplicationService applicationService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest req) {
        Teacher login = teacherService.login(username, password);
        if(login != null) {
            req.getSession().setAttribute("isStudent", false);
            req.getSession().setAttribute("user", login);
            return "redirect:/";
        } else {
            req.getSession().setAttribute("msg", "密码或者账号错误！");
            return "redirect:/toLogin";
        }
    }

    @RequestMapping("/receiveDropApplication")
    @ResponseBody
    public int receiveDropApplication(int id) {
        DropApplication dropApplication = applicationService.getDropApplication(id);
        studentService.deleteStudentById(dropApplication.getSid());
        return applicationService.receiveDropApplication(id);
    }

    @RequestMapping("/receiveSwitchApplication")
    @ResponseBody
    public int receiveSwitchApplication(int id) {
        SwitchApplication switchApplication = applicationService.getSwitchApplicationById(id);
        Student student = studentService.getStudentById(switchApplication.getSid());
        studentService.updateStudentById(new Student(student.getId(), student.getSid(), student.getName(), student.getPassword(), student.getSex(), student.getBirthday(), student.getPhoneNum(), student.getAddress(),student.getIntake(),student.getMajorid(),switchApplication.getClassid(), student.getTid(), 2));
        return applicationService.receiveSwitchApplication(id);
    }

    @RequestMapping("/refuseSwitchApplication")
    @ResponseBody
    public int refuseSwitchApplication(int id) {
        return applicationService.refuseSwitchApplication(id);
    }

    @RequestMapping("/refuseDropApplication")
    @ResponseBody
    public int refuseDropApplication(int id) {
        return applicationService.refuseDropApplication(id);
    }

    @RequestMapping("getAllStudent")
    public String getAllStudent(Model model, String tid) {
        Teacher teacher = teacherService.getTeacherByTid(tid);
        List<Student> students = null;
        Long count = studentService.getStudentCountByTid(tid);
        if(teacher.getRight() == 2) {
            students = studentService.getAllStudentsByTid(tid);
        } else {
            students = studentService.getAllStudents();
        }

        List<Stu> stus = new LinkedList<>();
        for(Student student : students) {
            Stu stu = new Stu();
            stu.setId(student.getId());
            stu.setSid(student.getSid());
            stu.setName(student.getName());
            stu.setIntake(student.getIntake());
            stu.setAddress(student.getAddress());
            stu.setBirthday(student.getBirthday());
            stu.setSex(student.getSex());
            stu.setPassword(student.getPassword());
            stu.setPhoneNum(student.getPhoneNum());
            stu.setMajor(majorService.getMajorById(student.getMajorid()));
            stu.setAClass(classService.getClassById(student.getClassid()));
            stu.setTeacher(teacherService.getTeacherByTid(student.getTid()));
            stus.add(stu);
        }
        model.addAttribute("studentCount", count);
        model.addAttribute("students", stus);
        return "showAllStudent";
    }

    @RequestMapping("toAddStudent")
    public String toAddStudent(Model model, String tid) {
        List<Class> classes = classService.getAllClassByTid(tid);
        model.addAttribute("classes", classes);
        return "addStudent";
    }

    @RequestMapping("toUpdateStudent")
    public String toUpdateStudent(int id,Model model, String tid) {
        List<Class> classes = classService.getAllClasses();
        model.addAttribute("classes", classes);
        Student student = studentService.getStudentById(id);
        model.addAttribute("student", student);
        return "updateStudent";
    }

    @RequestMapping("toReceiveSwitchApplication")
    public String toReceiveSwitchApplication(String tid, Model model) {
        List<SwitchApplication> switchApplications1 = applicationService.getAllSwitchApplicationByTid(tid);
        List<SwitchApp> switchApplications = new LinkedList<>();
        for(SwitchApplication switchApplication : switchApplications1) {
            SwitchApp switchApp = new SwitchApp(switchApplication.getId(), switchApplication.getAppTime(), switchApplication.getMsg(), switchApplication.getStatus(), studentService.getStudentById(switchApplication.getSid())
                    , classService.getClassById(switchApplication.getClassid()), teacherService.getTeacherByTid(switchApplication.getTid()));
            switchApplications.add(switchApp);
        }
        model.addAttribute("switchApplications", switchApplications);
        return "receiveSwitchApplication";
    }

    @RequestMapping("toReceiveDropApplication")
    public String toReceiveDropApplication(String tid, Model model) {
        List<DropApplication> dropApplications1 = applicationService.getAllDropApplicationByTid(tid);
        List<DropApp> dropApplications = new LinkedList<>();
        for(DropApplication dropApplication : dropApplications1) {
            DropApp dropApp = new DropApp(dropApplication.getId(), dropApplication.getAppTime(), dropApplication.getMsg(), dropApplication.getStatus(),studentService.getStudentById(dropApplication.getSid()), teacherService.getTeacherByTid(dropApplication.getTid()));
            dropApplications.add(dropApp);
        }
        model.addAttribute("dropApplications", dropApplications);
        return "receiveDropApplication";
    }

    @RequestMapping("/addStudent")
    @ResponseBody
    public int addStudent(String sid, String name, String password, int sex, String birthday, String phoneNum, String address, String intake, int classid, String tid) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday1 = null;
        Date intake1 = null;
        Teacher teacher = teacherService.getTeacherByTid(tid);
        try {
            birthday1 = formatter.parse(birthday);
            intake1 = formatter.parse(intake);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Student student = new Student(0, sid, name, password, sex, birthday1, phoneNum, address, intake1, teacher.getMajorid(), classid, tid, 1);
        return studentService.addStudent(student);
    }

    @RequestMapping("/updateStudent")
    @ResponseBody
    public int updateStudent(int id, String sid, String name, String password, int sex, String birthday, String phoneNum, String address, String intake, int classid, String tid) {
//        System.out.println(tid);
//        System.out.println(majorid);
//        System.out.println(classid);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday1 = null;
        Date intake1 = null;
        Teacher teacher = teacherService.getTeacherByTid(tid);
        try {
            birthday1 = formatter.parse(birthday);
            intake1 = formatter.parse(intake);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Student student = new Student(id, sid, name, password, sex, birthday1, phoneNum, address, intake1, teacher.getMajorid(), classid, tid, 1);
        return studentService.updateStudentById(student);
    }

    @RequestMapping("/deleteStudent")
    @ResponseBody
    public int deleteStudent(int id) {
        return studentService.deleteStudentById(id);
    }
}
